package com.campus.activity.controller;

import com.campus.activity.common.Result;
import com.campus.activity.entity.Feedback;
import com.campus.activity.service.FeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 反馈评价控制器
 * 
 * @author System
 * @since 2024
 */
@RestController
@RequestMapping("/feedbacks")
@Tag(name = "反馈评价管理", description = "活动反馈评价的提交和管理")
@Validated
@CrossOrigin(origins = "*")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @PostMapping
    @Operation(summary = "提交反馈评价", description = "学生提交活动反馈评价")
    public Result<Boolean> addFeedback(@Valid @RequestBody Feedback feedback) {
        boolean success = feedbackService.addFeedback(feedback);
        if (success) {
            return Result.success("反馈提交成功", true);
        } else {
            return Result.error("反馈提交失败，可能已经提交过反馈");
        }
    }

    @PutMapping("/{feedbackId}/reply")
    @Operation(summary = "回复反馈", description = "组织者回复学生的反馈评价")
    public Result<Boolean> replyFeedback(
            @Parameter(description = "反馈ID") @PathVariable Integer feedbackId,
            @Parameter(description = "回复内容") @RequestParam String replyContent) {
        boolean success = feedbackService.replyFeedback(feedbackId, replyContent);
        return success ? Result.success("回复成功", true) : Result.error("回复失败");
    }

    @GetMapping("/activity/{activityId}")
    @Operation(summary = "获取活动反馈", description = "获取指定活动的所有反馈评价")
    public Result<List<Feedback>> getFeedbackInfo(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        List<Feedback> feedbacks = feedbackService.getFeedbackInfo(activityId);
        return Result.success(feedbacks);
    }

    @GetMapping("/student/{studentId}")
    @Operation(summary = "获取学生反馈", description = "获取指定学生的所有反馈记录")
    public Result<List<Feedback>> getFeedbackByStudent(
            @Parameter(description = "学生ID") @PathVariable Integer studentId) {
        List<Feedback> feedbacks = feedbackService.getFeedbackByStudentId(studentId);
        return Result.success(feedbacks);
    }

    @GetMapping("/activity/{activityId}/statistics")
    @Operation(summary = "获取活动满意度统计", description = "获取指定活动的满意度统计信息")
    public Result<Map<String, Object>> getActivitySatisfactionStats(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        Map<String, Object> stats = feedbackService.getActivitySatisfactionStats(activityId);
        return Result.success(stats);
    }

    @GetMapping("/activity/{activityId}/unreplied")
    @Operation(summary = "获取未回复反馈", description = "获取指定活动的未回复反馈列表")
    public Result<List<Feedback>> getUnrepliedFeedback(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        List<Feedback> feedbacks = feedbackService.getUnrepliedFeedback(activityId);
        return Result.success(feedbacks);
    }

    @GetMapping("/activity/{activityId}/export")
    @Operation(summary = "导出反馈数据", description = "导出指定活动的反馈数据")
    public Result<List<Feedback>> exportFeedbackData(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        List<Feedback> feedbacks = feedbackService.exportFeedbackData(activityId);
        return Result.success(feedbacks);
    }

    @GetMapping("/check")
    @Operation(summary = "检查反馈状态", description = "检查学生是否已对活动进行反馈")
    public Result<Boolean> checkFeedbackStatus(
            @Parameter(description = "学生ID") @RequestParam Integer studentId,
            @Parameter(description = "活动ID") @RequestParam Integer activityId) {
        boolean hasFeedback = feedbackService.hasStudentFeedback(studentId, activityId);
        return Result.success(hasFeedback);
    }

    @GetMapping("/{feedbackId}")
    @Operation(summary = "获取反馈详情", description = "根据反馈ID获取反馈详细信息")
    public Result<Feedback> getFeedback(
            @Parameter(description = "反馈ID") @PathVariable Integer feedbackId) {
        Feedback feedback = feedbackService.getById(feedbackId);
        return feedback != null ? Result.success(feedback) : Result.notFound();
    }

    @DeleteMapping("/{feedbackId}")
    @Operation(summary = "删除反馈", description = "删除指定的反馈记录")
    public Result<Boolean> deleteFeedback(
            @Parameter(description = "反馈ID") @PathVariable Integer feedbackId) {
        boolean success = feedbackService.removeById(feedbackId);
        return success ? Result.success("删除成功", true) : Result.error("删除失败");
    }
}